package com.hzqy.service.catalog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.commons.utils.JsonUtils;
import com.hzqy.persistence.dao.catalog.CatalogDao;
import com.hzqy.persistence.dao.picture.PictureDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.web.vo.CmCatalogVo;
import com.hzqy.web.vo.CmPicturesVo;
import com.hzqy.web.vo.PmsZTreeVo;

@Service
public class CatalogServiceBean extends GenericServiceBean<CmCatalogVo, CatalogDao, Integer>{
	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	
	@Resource
	private CatalogDao catalogDao;
	@Resource
	private PictureDao pictureDao;

	@Override
	public CatalogDao getEntityDao() {
		return catalogDao;
	}
	
	public List<PmsZTreeVo> selectCatalogList(CmCatalogVo catalogVo) {
		List<PmsZTreeVo> ptvList = new ArrayList<PmsZTreeVo>();
		List<CmCatalogVo> prvList = catalogDao.selectCatalogList(catalogVo);
		for(int i=0; i<prvList.size();i++){
			PmsZTreeVo ptv = new PmsZTreeVo();
			ptv.setId(prvList.get(i).getF_catalog_id()+"");
			ptv.setName(prvList.get(i).getF_catalog_name());
			ptv.setpId(prvList.get(i).getF_catalog_parentid()+"");
			ptv.setUrl(prvList.get(i).getF_catalog_url());
			ptvList.add(ptv);
		}
		return ptvList;
	}
	
	public int deleteCatalog(int f_catalog_id) {
		catalogDao.deleteCatalog(f_catalog_id);
		return 1;
	}
	
	public int insertCatalog(CmCatalogVo catalogVo,String picJson) {
		int flag = 0;
		if(catalogVo==null || catalogVo.getF_catalog_name()==null) {
			flag = -1;
		}
		if(flag ==0) {
			List<CmPicturesVo> picList = JsonUtils.jsonToPicVo(picJson);
			catalogDao.insertCatalog(catalogVo);
			int f_catalog_id = catalogVo.getF_catalog_id();
			for(int i=0;i<picList.size();i++) {
				CmPicturesVo cmPicturesVo = new CmPicturesVo();
				cmPicturesVo = picList.get(i);
				cmPicturesVo.setF_cp_relationid(f_catalog_id);
				pictureDao.insertPic(cmPicturesVo);
			}
			return 1;
		}
		return flag;
	}
	
	public CmCatalogVo selectCatalog(int f_catalog_id) {
		return catalogDao.selectCatalog(f_catalog_id);
	}
	
	public int updateCatalog(CmCatalogVo catalogVo,String picJson,String picType) {
		int flag = 0;
		if(catalogVo==null || catalogVo.getF_catalog_name()==null) {
			flag = -1;
		}
		if(flag ==0) {
			List<CmPicturesVo> picList = JsonUtils.jsonToPicVo(picJson);
			String listPicId = JsonUtils.jsonToPicIdList(picJson);
			int f_cv_relationid = catalogVo.getF_catalog_id();
			Map<String,Object> picMap = new HashMap<String, Object>();
			picMap.put("listPicId", listPicId);
			picMap.put("f_cp_relationid", f_cv_relationid);
			picMap.put("picType", picType);
			
			catalogDao.updateCatalog(catalogVo);
			pictureDao.deletePic(picMap);
			for(int i=0;i<picList.size();i++) {
				CmPicturesVo cmPicturesVo = new CmPicturesVo();
				cmPicturesVo = picList.get(i);
				cmPicturesVo.setF_cp_relationid(f_cv_relationid);
				pictureDao.insertPic(cmPicturesVo);
			}
			return 1;
		}
		return flag;
	}

	public List<PmsZTreeVo> selectCatalogIndustryList(CmCatalogVo catalogVo) {
		List<PmsZTreeVo> ptvList = new ArrayList<PmsZTreeVo>();
		List<CmCatalogVo> prvList = catalogDao.selectCatalogIndustryList(catalogVo);
		for(int i=0; i<prvList.size();i++){
			PmsZTreeVo ptv = new PmsZTreeVo();
			ptv.setId(prvList.get(i).getF_catalog_id()+"");
			ptv.setName(prvList.get(i).getF_catalog_id()+"_"+prvList.get(i).getF_catalog_name());
			ptv.setpId(prvList.get(i).getF_catalog_parentid()+"");
			ptv.setUrl(prvList.get(i).getF_catalog_url());
			ptv.setProid(prvList.get(i).getF_catalog_productid());
			ptv.setOrgid(prvList.get(i).getF_catalog_orgid());
			ptvList.add(ptv);
		}
		return ptvList;
	}
	
	public List<CmCatalogVo> selectCatalogContentList(String catalogIds) {
		CmCatalogVo cmCatalogVo = new CmCatalogVo();
		cmCatalogVo.setCatalogIds(catalogIds);
		return catalogDao.selectCatalogContentList(cmCatalogVo);
	}
	
}
